package com.fin.zw.aiqas.controller;

import com.fin.zw.aiqas.service.AsyncTestService;
import com.fin.zw.aiqas.util.AudioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: qijigui
 * @CreateDate: 2019/10/17 14:55
 * @Description:
 */

@RestController
@RequestMapping("/testController")
public class TestController {

    @Autowired
    AsyncTestService asyncTestService;

    @GetMapping("/test")
    public void test() {
        String result = AudioUtil.audioToText("E:\\data\\未约定还款时间1.wav");
        System.out.println(result);
    }

    @GetMapping("/asyncTest")
    public Integer asyncTest() {
        for (int i = 0; i < 1000; i++) {
            System.out.println("正在进行第" + i + "个创建");
            asyncTestService.printInfo();
        }
        System.out.println("当前的线程数" + getThreadNumber());
        return getThreadNumber();
    }

    private Integer getThreadNumber() {

        ThreadGroup group = Thread.currentThread().getThreadGroup();
        ThreadGroup topGroup = group;
        // 遍历线程组树，获取根线程组
        while (group != null) {
            topGroup = group;
            group = group.getParent();
        }
        // 激活的线程数加倍
        int estimatedSize = topGroup.activeCount() * 2;
        Thread[] slackList = new Thread[estimatedSize];
        // 获取根线程组的所有线程
        int actualSize = topGroup.enumerate(slackList);
        // copy into a list that is the exact size
        Thread[] list = new Thread[actualSize];
        System.arraycopy(slackList, 0, list, 0, actualSize);
        return list.length;
    }
}
